package com.aier.user.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.common.common.CommonSession;
import com.aier.common.common.Constant;
import com.aier.common.common.ICommonDAO;
import com.aier.common.common.ImageUtil;
import com.aier.common.common.SystemConfigStatic;
import com.aier.user.entity.Follow;
import com.aier.user.entity.FollowGroup;
import com.aier.user.entity.Fresh;
import com.aier.user.entity.FreshComment;
import com.aier.user.entity.FreshFavorite;
import com.aier.user.entity.User;
import com.aier.user.service.IFindPeopleService;
import com.aier.user.service.IHomeForumsService;

@Scope("prototype")
@Component("userHomeForumsAction")
public class UserHomeForumsAction extends UserBaseAction{
    
    @Resource(name="homeForumsService")
    private IHomeForumsService homeForumsService;
    @Resource(name="findPeopleService")
    private IFindPeopleService findPeopleService;
    
    /**
     * 首页/新鲜事
     */
    private static final long serialVersionUID = 1L;
    
    // 错误码
    private int s;
    // 当前页数
    private int page;
    // 每页的记录数
    private int page_size;
    // 总页数
    private int page_count;
    // 总记录数
    private int count;
    
    // 图片名称
    private String imageName;
    
    // 文字内容
    private String content;
    // 被@人的id
    private String special;
    // 新鲜事的可见度
    private Short power;
    // 如果是分组可见，则需要分组id参数
    private Long fgid;
    
    //新鲜事的id
    private Long fid;
    
    // 类型：0赞1取消赞
    private int ptype;
    // 赞的次数
    private Long pcount;
    
    // 收藏记录的id
    private Long ffid;
    
    // 获取所有的关注
    private List<Follow> followList;
    
    // 新鲜事列表
    private List<Fresh> freshList;
    
    // 所有的关注分组
    private List<FollowGroup> fgList;
    
    // 所有的收藏
    private List<FreshFavorite> ffList;
    
    // 某条新鲜事的评论
    private List<FreshComment> fcList;
    // 评论或回复时是否转发（0不转发1转发）
    private int flag;
    
    // 转发时是否评论(是否评论给原作者)
    private int cflag;
    
    // 评论的id
    private Long fcid;
    // 评论的次数
    private Long ccount;
    // 转发的次数
    private Long tcount;
    
    private User userInfo;
    
    // 登录的用户的id
    private Long uid;
    
    // 回复的对象的内容
    private String rcontent;
    
    // 客户端上传的图片
    private File[] image;
    // 上传图片的名称或路径
    private String[] photoName;
    
    // 评论的新鲜事或评论的作者的uid
    private Long rfcuid;
    private Short ctype; // 评论或回复的类型（1评论2回复）
    
    @Override
    public String execute(){
        // 查询个人资料
        userInfo = findPeopleService.searchSelfInfo((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 删除图片
     * @return
     */
    public String deleteImage() {
        File fo = new File(SystemConfigStatic.fresh_upload_url + "origin/" +imageName);
        if (fo.exists()) {
            fo.delete();
        }
        File fs = new File(SystemConfigStatic.fresh_upload_url + "small/" +imageName);
        if (fs.exists()) {
            fs.delete();
        }
        File fb = new File(SystemConfigStatic.fresh_upload_url + "big/" +imageName);
        if (fb.exists()) {
            fb.delete();
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 发布新鲜事
     * @return
     */
    public String publishFresh() {
        
        s = homeForumsService.insertFresh((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID), 
                content, imageName, special,power,fgid);
        
        return SUCCESS;
    }
    
    /**
     * 转发新鲜事
     * @return
     */
    public String transmitFresh() {
        
        s = homeForumsService.insertOtherFresh((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID), 
                content, special, power, fgid, fid,cflag);
        Fresh f = homeForumsService.searchFresh(fid, 1);
        // 获取新鲜事的转发的次数
        tcount = f.getTcount();
        
        return SUCCESS;
    }
    
    /**
     * 评论新鲜事
     * @return
     */
    public String commentFresh() {
        s = homeForumsService.insertFreshComment(
                (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID), fid, rfcuid, content,rcontent,ctype,flag,cflag,special);
        Fresh f = homeForumsService.searchFresh(fid, 1);
        // 获取新鲜事的评论的次数
        ccount = f.getCcount();
        return SUCCESS;
    }
    
    /**
     * 浏览新鲜事
     * @return
     */
    public String browerFresh() {
        s = homeForumsService.updateFreshBcount(fid);
        return SUCCESS;
    }
    
    /**
     * 赞或取消赞新鲜事
     * @return
     */
    public String praiseFresh() {
        s = homeForumsService.updateFreshPraise((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID),fid, ptype);
        Fresh f = homeForumsService.searchFresh(fid, 1);
        // 获取新鲜事的赞的次数
        pcount = f.getPcount();
        
        return SUCCESS;
    }
    
    /**
     * 收藏新鲜事
     * @return
     */
    public String favoriteFresh() {
        s = homeForumsService.insertFavoriteFresh((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID), fid);
        return SUCCESS;
    }
    
    /**
     * 取消收藏
     * @return
     */
    public String deleteFavoriteFresh() {
        s = homeForumsService.deleteFavoriteFresh(ffid);
        return SUCCESS;
    }
    
    /**
     * 根据uid和fid取消收藏
     * @return
     */
    public String deleteFFByUid() {
        s = homeForumsService.deleteFFByUid((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID), fid);
        return SUCCESS;
    }
    
    /**
     * 获取所有收藏
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchFavorite() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = null;
        if (uid != null) {
            map = homeForumsService.searchFavorite(page, page_size, uid);
        } else {
            map = homeForumsService.searchFavorite(page, page_size, 
                    (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        }
        
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            
            ffList = (List<FreshFavorite>) map.get(ICommonDAO.OBJ_LIST);
            
            if (null != ffList && ffList.size() > 0) {
                for (FreshFavorite ff : ffList) {
                    if (ff.getFresh().getType() != 0) {
                        ff.getFresh().setFresh(homeForumsService.searchFresh(ff.getFresh().getRelativeid(),1));
                    }
                    // 是否已赞过（1赞过0没赞）
                    if (null != homeForumsService.searchPraiseByCon(
                            (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID), ff.getFresh().getFid())) {
                        ff.getFresh().setPflag((short)1);
                    } else {
                        ff.getFresh().setPflag((short)0);
                    }
                }
            }
                
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 查出所有的关注（@时）
     * @return
     */
    public String searchAllFollow() {
        followList = homeForumsService.searchAllFollow((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 查询所有的新鲜事
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchAllFresh() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = homeForumsService.searchAllFresh(page, page_size, 
                (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer)map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            freshList = (List<Fresh>) map.get(ICommonDAO.OBJ_LIST);
                
            if (null != freshList && freshList.size() > 0) {
                for (Fresh fresh : freshList) {
                    if (fresh.getType() == 1) {
                        fresh.setFresh(homeForumsService.searchFresh(fresh.getRelativeid(),1));
                    }
                    // 是否已赞过（1赞过0没赞）
                    if (null != homeForumsService.searchPraiseByCon(
                            (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID), fresh.getFid())) {
                        fresh.setPflag((short)1);
                    } else {
                        fresh.setPflag((short)0);
                    }
                    // 是否已收藏
                    if (null != homeForumsService.searchFavoriteByCon(
                            (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID), fresh.getFid())) {
                        fresh.setFflag((short)1);
                    } else {
                        fresh.setFflag((short)0);
                    }
                        
                }
            }
                
        }
        uid = (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID);
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 获取某条新鲜事的评论
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchCommentByFid() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = homeForumsService.searchCommentByFid(page, page_size, fid);
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            
            fcList = (List<FreshComment>) map.get(ICommonDAO.OBJ_LIST);
            if (fcList != null) {
                for (FreshComment fc : fcList) {
                    fc.setFresh(homeForumsService.searchFresh(fc.getFid(),1));
                }
            }
                
        }
        s = 1;
        return SUCCESS;
    }
    
    
    /**
     * 获取所有的动态信息
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchFollowDo() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = homeForumsService.searchFollowDo(
                page, page_size, (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        followList = new ArrayList<Follow>();
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            
            followList = (List<Follow>) map.get(ICommonDAO.OBJ_LIST);
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 获取密友的动态信息
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchFriendDo() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = homeForumsService.searchFriendDo(
                page, page_size, (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        followList = new ArrayList<Follow>();
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            
            followList = (List<Follow>) map.get(ICommonDAO.OBJ_LIST);
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 删除评论或回复
     * @return
     */
    public String deleteComment() {
        s = homeForumsService.deleteComment(fcid);
        return SUCCESS;
    }
    
    /**
     * 删除新鲜事
     * @return
     */
    public String deleteFresh() {
        s = homeForumsService.deleteFresh(fid);
        return SUCCESS;
    }
    
    
    /**
     * 客户端发布新鲜事
     * @return
     */
    public String publishFreshApp() {
        String picName = "";
        if (null != image) {
            for (int i=0;i<image.length;i++) {
                File file = image[i];
                
                String fileName = ImageUtil.uploadImage(file, SystemConfigStatic.fresh_upload_url, photoName[i], 1);
                
                if (!"".equals(picName)) {
                    picName += ",";
                }
                picName += fileName;
            }
        }
        s = homeForumsService.insertFresh((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID), 
                content, picName, special,power,fgid);
        
        return SUCCESS;
    }
    
    public void setHomeForumsService(IHomeForumsService homeForumsService) {
        this.homeForumsService = homeForumsService;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getS() {
        return s;
    }

    public void setSpecial(String special) {
        this.special = special;
    }

    public void setPower(Short power) {
        this.power = power;
    }

    public void setFgid(Long fgid) {
        this.fgid = fgid;
    }

    public void setFid(Long fid) {
        this.fid = fid;
    }

    public void setPtype(int ptype) {
        this.ptype = ptype;
    }

    public void setFfid(Long ffid) {
        this.ffid = ffid;
    }

    public List<Follow> getFollowList() {
        return followList;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public List<Fresh> getFreshList() {
        return freshList;
    }

    public List<FollowGroup> getFgList() {
        return fgList;
    }

    public List<FreshFavorite> getFfList() {
        return ffList;
    }

    public List<FreshComment> getFcList() {
        return fcList;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    public void setFcid(Long fcid) {
        this.fcid = fcid;
    }

    public void setFindPeopleService(IFindPeopleService findPeopleService) {
        this.findPeopleService = findPeopleService;
    }

    public User getUserInfo() {
        return userInfo;
    }

    public Long getUid() {
        return uid;
    }

    public String getImageName() {
        return imageName;
    }

    public void setImageName(String imageName) {
        this.imageName = imageName;
    }

    public Long getFgid() {
        return fgid;
    }

    public Long getPcount() {
        return pcount;
    }

    public void setCflag(int cflag) {
        this.cflag = cflag;
    }

    public Long getCcount() {
        return ccount;
    }

    public Long getTcount() {
        return tcount;
    }

    public void setRcontent(String rcontent) {
        this.rcontent = rcontent;
    }

    public int getPage_count() {
        return page_count;
    }

    public int getCount() {
        return count;
    }

    public void setImage(File[] image) {
        this.image = image;
    }

    public void setPhotoName(String[] photoName) {
        this.photoName = photoName;
    }

    public void setRfcuid(Long rfcuid) {
        this.rfcuid = rfcuid;
    }

    public void setCtype(Short ctype) {
        this.ctype = ctype;
    }

    public void setUid(Long uid) {
        this.uid = uid;
    }
}
